--[[
  sip
]]
require("apm_core")
require("ApmXml")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local function parse_sdp(ApmData, s_sip_body)
    for k, v in string.gmatch(s_sip_body, "([a-zA-z]+)=(.-)\r") do
        --        logInfo("[" .. k .. "=" .. v .. "]")
        if k == "s" then
            if v == "Play" then
                ApmData.replaceField("transaction.type_code", "视频预览")
                ApmData.replaceField("transaction.type_name", "视频预览")
                logInfo("视频预览")
            elseif v == "Playback" then
                ApmData.replaceField("transaction.type_code", "视频回放")
                ApmData.replaceField("transaction.type_name", "视频回放")
                logInfo("视频回放")
            end
        end
    end
end

local function parse_keepalive(ApmData, s_sip_body)
    local t_xml_tree = ApmXml.load(s_sip_body, "UTF-8");

    if t_xml_tree == nil then return false end

    if ApmXml.getValue(t_xml_tree, "/Notify") == nil then return end

    local s_cmd_type = ApmXml.getValue(t_xml_tree, "/Notify/CmdType/text()")
    local s_device_id = ApmXml.getValue(t_xml_tree, "/Notify/DeviceID/text()")

    if s_cmd_type == "Keepalive" then
        ApmData.replaceField("transaction.type_code", "心跳检测服务")
        ApmData.replaceField("transaction.type_name", "心跳检测服务")
        logInfo("/Notify/CmdType:[Keepalive] %s", "心跳检测服务")
        if s_device_id ~= nil then
            ApmData.replaceField("transaction.code", s_device_id)
        end
    end
end

local function parse_catalog(ApmData, s_sip_body)
    local t_xml_tree = ApmXml.load(s_sip_body, "UTF-8");
    if t_xml_tree == nil then return false end

    if ApmXml.getValue(t_xml_tree, "/Notify") ~= nil then

        local s_cmd_type = ApmXml.getValue(t_xml_tree, "/Notify/CmdType/text()")
        local s_device_id = ApmXml.getValue(t_xml_tree, "/Notify/DeviceID/text()")
        if s_cmd_type == "Catalog" then
            ApmData.replaceField("transaction.type_code", "资源共享服务")
            ApmData.replaceField("transaction.type_name", "资源共享服务")
            logInfo("/Notify/CmdType:[RecordInfo] %s", "资源共享服务")
            if s_device_id ~= nil then
                ApmData.replaceField("transaction.code", s_device_id)
            end
        else
            logInfo("/Notify/CmdType:[%s] unknown", s_cmd_type)
        end

    elseif ApmXml.getValue(t_xml_tree, "/Query") ~= nil then

        local s_cmd_type = ApmXml.getValue(t_xml_tree, "/Query/CmdType/text()")
        local s_device_id = ApmXml.getValue(t_xml_tree, "/Query/DeviceID/text()")
        if s_cmd_type == "RecordInfo" then
            ApmData.replaceField("transaction.type_code", "录像查询")
            ApmData.replaceField("transaction.type_name", "录像查询")
            logInfo("/Query/CmdType:[RecordInfo] %s", "录像查询")
            if s_device_id ~= nil then
                ApmData.replaceField("transaction.code", s_device_id)
            end
        else
            logInfo("/Query/CmdType:[%s] unknown", s_cmd_type)
        end

    end

end

--- main
function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 5060 then return end
    if ApmData.getMsgType() ~= 1 then return end

    local s_sip_method, s_sip_body

    s_sip_method = ApmData.getField("request.sip.method", 1)
    s_sip_body = ApmData.getField("request.sip.body", 1)

    --    logInfo("method:[%s] body:[%s]", s_sip_method, s_sip_body)
    if s_sip_body ~= nil then
        if s_sip_method == "INVITE" then
            parse_sdp(ApmData, s_sip_body)
        elseif s_sip_method == "MESSAGE" then
            parse_keepalive(ApmData, s_sip_body)
        elseif s_sip_method == "NOTIFY" then
            parse_catalog(ApmData, s_sip_body)
        end
    end

    return;
end
